#include "SkillChongZhuang.h"

#include "base/core/log.h"
#include "base/core/com_define.h"
#include "Character/MovePart.h"
#include "Character/SkillPart.h"
#include "Character/newAI/AiLogicUtility.h"
#include "Scene/MapMgr.h"
SkillChongZhuang::SkillChongZhuang()
{

}
SkillChongZhuang::~SkillChongZhuang()
{

}

void SkillChongZhuang::refreshData()
{
	SkillBase::refreshData();
	m_listFindTarget.clear();
}


void SkillChongZhuang::OnDamageTimedoPlayer(uint32_t skillId, cList & vtargetList)
{

	Creature * pCreature = nullptr;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);

	m_pPart->KillTimedo(m_nGlobelID);

	//目标要传到指定点
	pCreature = m_pPart->GetMaster();
	if (pCreature)
	{
		MovePart * pPart = dynamic_cast<MovePart*>(pCreature->GetPart(PART_MOVE));
		if (pPart)
		{
			std::vector<int32_t> result;
			CommonApi::SplitStrToVecInt(pSkill->_pSkillmeta->displaceDistance, ",", &result);
			if (!result.empty())
			{
				Point3<float> targetPos = m_pPart->GetMasterPos() + m_pPart->GetMasterDir()*result[0] / 1000.0f;
				pPart->Teleporting(targetPos);
				LogDebugFmtPrint("skill chongzhuang pos(%f,%f,%f)", targetPos.x, targetPos.y, targetPos.z);
			}
		}
	}
	
	//计算技能结果
	m_pPart->SkillResult(pSkill->_Id, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, vtargetList);
	//技能一次释放完成，需要删通知删除
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
}

void SkillChongZhuang::OnDamageTimedo(uint32_t skillId)
{

	CharIDType targetCid = 0;
	stSkillInfo *pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill);

	m_pPart->KillTimedo(m_nGlobelID);

	//根据时间点来取找当前目标
	if (Range_none_target > pSkill->_pSkillmeta->rangeType)
	{
		MMOLOG_PROCESS_ERROR(m_curDamageCount < m_vTargetCids.size());
		targetCid = m_vTargetCids[m_curDamageCount];
	}

	if (m_curDamageCount == 0)
	{
		m_pPart->FindCreatures(pSkill->_pSkillmeta, m_pPart->GetMasterPos(), m_pPart->GetMasterDir(), targetCid, m_targetPos, m_curDamageCount, m_focoTime, m_listFindTarget);

		//目标要传到指定点
		Creature * pCreature = m_pPart->GetMaster();
		if (pCreature)
		{
			MovePart * pPart = dynamic_cast<MovePart*>(pCreature->GetPart(PART_MOVE));
			if (pPart)
			{
				std::vector<int32_t> result;
				CommonApi::SplitStrToVecInt(pSkill->_pSkillmeta->displaceDistance, ",", &result);
				if (!result.empty())
				{
					Point3<float> targetPos ;// m_pPart->GetMasterPos() + m_pPart->GetMasterDir()*result[0] / 1000.0f;
					float dict = result[0] / 1000.0f;
					Point3<float> srcPos = m_pPart->GetMasterPos();
					Map* pMap = g_GetMapMgr()->GetMap(m_pPart->GetMaster()->GetMapId());
					bool ret = false;
					if (pMap)
					{
						//for (float d = dict; d >= 0.1f; d -= 0.1f)
						Point3<float> tmpPos = srcPos + m_pPart->GetMasterDir() * 0.1f;
						for (float d = 0.1f; d <=dict; d += 0.1f)
						{
							ret = pMap->FindNearestPos(tmpPos.x, tmpPos.z, tmpPos.y, &targetPos.x, &targetPos.z, &targetPos.y, nullptr);
							if (!ret)
							{
								break;
							}
							tmpPos = srcPos + m_pPart->GetMasterDir() * d;
						}
					}

					//找一个可以用的点
					//Point3<float> lastTargetPos = AiLogicUtility::GetPosInStraight(m_pPart->GetMaster()->GetMapId(), m_pPart->GetMasterPos(), targetPos, result[0] / 1000.0f);

					pPart->Teleporting(targetPos);
					LogDebugFmtPrint("skill chongzhuang pos(%f,%f,%f)", targetPos.x, targetPos.y, targetPos.z);
				}
			}
		}
	}

	//计算技能结果
	m_pPart->SkillResult(pSkill->_Id, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, m_listFindTarget);

	if (pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{
		//技能一次释放完成，需要删通知删除
		m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
}
